<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<?php
session_start();
require_once 'code/Tasks.php';
require_once 'code/Util.php';
require_once 'code/DB.php';
?>
<html>
    <head>
        <link rel="shortcut icon" href="images/iTrack.ico"> 
        <META HTTP-EQUIV="CACHE-CONTROL" CONTENT="PRIVATE">
        <META HTTP-EQUIV="CONTENT-LANGUAGE" CONTENT="en-US">
        <META NAME="DESCRIPTION" CONTENT="Tasks application home page">
        <META NAME="KEYWORDS" CONTENT="iTrack">
        <META NAME="COPYRIGHT" CONTENT="&copy; iTrack 2013">

        <title>iTrack - Create Task</title>

        <link href="assets/css/bootstrap.css" rel="stylesheet">
        <style type="text/css">
            body {
                padding-top: 60px;
            }
        </style>
        <link href="assets/css/bootstrap-responsive.css" rel="stylesheet">
        <script src="assets/js/jquery.js"></script>        
        <script type="text/javascript" src="jquery/jquery-ui-1.8.13.custom.min.js"></script>


        <script type="text/javascript">
            $(document).ready(function() {                
                $("#txtTask").focus();
                refreshTaskUpdates();
                
                document.getElementById("txtTask").value = document.getElementById("txtTask").value;
                document.getElementById("txtTask").focus();  
                document.getElementById("txtTaskUpdates").focus();
            });
            
            function saveTask() {
                var txtTask = document.getElementById("txtTask");
                if(txtTask.value != "") {
                    document.getElementById("frm").submit();
                } else {
                    alert('Cannot save task without description !');
                    document.getElementById("txtTask").focus();
                    return false;
                }            
            }
            
            function addTaskUpdate() {
                var txtTaskUpdates = $("#txtTaskUpdates").val();
                var userid = $("#userid").val();
                var taskid = $("#taskid").val();
            
                if(txtTaskUpdates != "") {
                    // make ajax call
                    $("#loading").show();
                
                    $.ajax({         
                        url: 'controller.php', 
                        type: 'GET',
                        timeout:4000,
                        dataType: 'html',
                        data: { 
                            'action': 'addtaskupdate',
                            'userid': userid,
                            'projectid':'1',
                            'taskid' : taskid,
                            'update' : txtTaskUpdates
                        },         
                        success: function(data) {
                            $("#txtTaskUpdates").val("");
                        
                            $("#loading").hide();
                            //alert(data);
                            if (data != null) {
                                if ((data != null) && (data != "")) {
                                    $("#updates").html(data);
                                } else {
                                    $("#updates").html("No updates available yet !");
                                }
                            } else {
                                //alert(data);
                            }                                                               
                        }, error: function (xhr, ajaxOptions, thrownError) {
                            $("#loading").hide();
                            //alert(xhr.status);
                            //alert(thrownError);
                        }
                    });
                } else {
                    alert('Please add some update to save !');                
                }
                document.getElementById("txtTaskUpdates").focus();
                return false;
            }
        
            function refreshTaskUpdates() {
                var taskid = $("#taskid").val();
        
                $.ajax({         
                    url: 'controller.php', 
                    type: 'GET',
                    timeout:4000,
                    dataType: 'html',
                    data: { 
                        'action': 'refreshtaskupdate',
                        'taskid' : taskid
                    },         
                    success: function(data) {
                        $("#loading").hide();
                        //alert(data);
                        if ((data != null) && (data != "")) {
                            $("#updates").html(data);
                        } else {
                            $("#updates").html("No updates available yet !");
                        }                                                               
                    }, error: function (xhr, ajaxOptions, thrownError) {
                        $("#loading").hide();
                        alert(xhr.status);
                        alert(thrownError);
                    }
                });
            }
            
            function CreateProjectPopup() {
                document.getElementById("txtProjectName").focus();
                $('#myModal').modal('show');
            }
		
            function SaveProject() {
                var projectid = $("#projectid").val();
                var projectname = $("#txtProjectName").val();
                var projectdesc = $("#txtProjectDescription").val();
                var visibility = $("input:radio[name=Visibility]:checked").val();
		
                if(projectname != "") {
                    $.ajax({         
                        url: "../Controller", 
                        type: "POST",
                        dataType: "json",                
                        data: { 
                            "action": "saveproject",
                            "userid": $("#userid").val(),
                            "projectid":projectid,
                            "projectname" : projectname,
                            "description" : projectdesc,
                            "visibility"  : visibility
                        },         
                        success: function(data) { 
                            if (data != null) {
                                if(data.status == "success") {
                                    $('#myModal').modal('hide');
                                    window.location ="viewproject.php?projectid="+projectid;
                                } else {
                                    alert("Error saving project !");
                                }
                            }                                                               
                        },
                        error:function (xhr, ajaxOptions, thrownError){
                            //window.location = "error.php";
                        }
                    });
                }
                return false;
            }
            
            function DeleteProject() {
                if(confirm("Deleting Project. Are you sure?") == true) {
                    $.ajax({         
                        url: "../Controller", 
                        type: "POST",
                        dataType: "json",                
                        data: { 
                            "action": "deleteproject",
                            "userid": $("#userid").val(),
                            "projectid":$("#projectid").val()
                        },         
                        success: function(data) { 
                            if (data != null) {
                                if(data.status == "success") {
                                    $('#myModal').modal('hide');
                                    //window.location ="projects.php" + "?random="+new Date().getTime();
                                } else {
                                    alert("Error deleting project !");
                                }
                            }                                                               
                        },
                        error:function (xhr, ajaxOptions, thrownError){
                            //window.location ="projects.php" + "?random="+new Date().getTime();
                        }
                    });
                }
		
                return false;
            }
        </script>
        <style type="text/css">
            .sidebar-nav {
                padding: 9px 0;
            }
            .ckeditor {
                width: 100%;
                margin: 5px 0;
                padding: 3px;
                -webkit-box-sizing: border-box;
                -moz-box-sizing: border-box;
                box-sizing: border-box;
            }
        </style>    
    </head>
    <body>
        <?php
        if (isset($_SESSION['userid'])) {
            $userid = $_SESSION['userid'];
            $projectid = $_GET['projectid'];
            ?>

            <?php
            Util::getNavBar();
            ?>

            <div class="container-fluid">
                <div class="row-fluid">
                    <div class="span2">
                        <div class="well sidebar-nav">
                            <?php
                            echo Util::getLeftNavList('Projects');
                            ?>
                        </div><!--/.well -->
                    </div><!--/span-->
                    <div class="span10">          
                        <div class="row-fluid">
                            <div class="span12">
                                <?php
                                $tasksObj = new Tasks();
                                $projects = array();

                                if ($projectid > 0) {
                                    $projects = $tasksObj->getAllProjects($userid, $projectid);
                                }

                                $projectname = "N/A";
                                $description = "N/A";
                                //echo count($projects);
                                if (count($projects) > 0) {
                                    foreach ($projects as $db_field) {
                                        $projectname = $db_field['project'];
                                        $description = $db_field['description'];
                                    }
                                    ?>
                                    <h2><a href="viewproject.php?projectid=<?php echo $projectid ?>"><?php echo $projectname ?></a>
                                        &nbsp;&nbsp;<a class="btn btn-mini" type="button" href="javascript:void(0);" onclick="CreateProjectPopup()"><i class="icon icon-edit"></i>&nbsp; Edit</a>
                                    </h2>
                                    <p class="muted"><?php echo $description ?></p>

                                    <h3>Update Task</h3>
                                    <?php
                                    try {
                                        if (isset($_GET["taskid"])) {
                                            $db = new DB();
                                            $sql = "SELECT * FROM tasks WHERE isdeleted = 0 AND id = " . $_GET["taskid"];
                                            $db->query($sql);
                                            $result = $db->resultset();
                                            foreach ($result as $db_field) {
                                                $description = $db_field['description'];
                                                $type = $db_field['type'];
                                                $status = $db_field['status'];
                                                ?>

                                                <form id="frm" name="frm" action="savetask.php" method="POST" >
                                                    <input type="hidden" id="userid" name="userid" value="<?php echo $_SESSION['userid']; ?>">
                                                    <input type="hidden" id="projectid" name="projectid" value="<?php echo $_GET['projectid']; ?>">
                                                    <input type="hidden" id="taskid" name="taskid" value="<?php echo $_GET["taskid"]; ?>">
                                                    <input type="hidden" id="operation" name="operation" value="edit">

                                                    <table class="table-condensed" width="100%" border="0">
                                                        <tbody>
                                                            <tr>
                                                                <td width="60%" style="vertical-align: baseline">
                                                                    <label class="control-label heading" for="txtTask"><abbr title="Task Description">Task Description</abbr></label>
                                                                    <div class="controls">
                                                                        <textarea style="width:98%;" id="txtTask" name="txtTask" rows="5" readonly 
                                                                                  onfocus="this.value = this.value;"><?php echo $description; ?></textarea>
                                                                    </div>
                                                                </td>
                                                                <td>
                                                                    <div class="control-group">
                                                                        <label class="control-label heading" for="selType"><abbr title="Task Type">Type</abbr></label>
                                                                        <div class="controls">
                                                                            <?php
                                                                            Util::getTaskTypeSelBox('selType', 'selType', $type);
                                                                            ?>                                                                            
                                                                        </div>
                                                                    </div>
                                                                    <div class="control-group">
                                                                        <label class="control-label heading" for="selStatus"><abbr title="Task Status">Status</abbr></label>
                                                                        <div class="controls">
                                                                            <?php
                                                                            Util::getTaskStatusSelBox('selStatus', 'selStatus', $status);
                                                                            ?>
                                                                        </div>
                                                                    </div>
                                                                    <div class="controls">
                                                                        <button type="button" class="btn btn-primary" onclick="saveTask()"><i class="icon-envelope"></i>&nbsp;&nbsp; Save &nbsp;</button>
                                                                        <a class="btn btn-link" href="viewproject.php?projectid=<?php echo $_GET['projectid']; ?>"><i class="icon-plus-sign"></i>&nbsp;&nbsp; Cancel &nbsp;</a>
                                                                    </div>
                                                                </td>
                                                            </tr>

                                                            <tr>
                                                                <td colspan="2">
                                                                    <label class="control-label heading" for="txtTask"><abbr title="Updates">Task Updates</abbr></label>
                                                                    <div class="controls">
                                                                        <textarea style="width:98%;" id="txtTaskUpdates" name="txtTaskUpdates" rows="3"></textarea>
                                                                    </div>
                                                                </td>
                                                            </tr>
                                                            <tr>
                                                                <td colspan="2">
                                                                    <button type="button" class="btn btn-success btn-mini" onclick="addTaskUpdate()"><i class="icon-envelope"></i>&nbsp;&nbsp; Add Update &nbsp;</button>
                                                                    &nbsp;
                                                                    <span><img id="loading" alt="saving ..." src="images/loader_01.gif" border="0" style="display:none;"></span>
                                                                </td>
                                                            </tr>
                                                            <tr>
                                                                <td colspan="2">
                                                                    <div id="updates" class="" style="width: 100%"></div>
                                                                </td>
                                                            </tr>
                                                        </tbody>
                                                    </table>
                                                </form>

                                                <?php
                                            }
                                        }
                                    } catch (Exception $e) {
                                        print "Exception : " . $e . getMessage();
                                    }
                                }
                                ?>

                            </div><!--/span-->          
                        </div><!--/row-->

                    </div><!--/span-->
                </div><!--/row-->

                <!-- Include footer -->
                <?php include_once 'footer.php'; ?>
            </div>

            <!-- Dialogs -->
            <div class="modal hide fade in" id="myModal" tabindex="-1"
                 role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"
                 style="width: 25em">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal"
                            aria-hidden="true">&times;</button>
                    <h3 id="myModalLabel"><i class="icon-bullhorn"></i>&nbsp;Edit Project</h3>
                </div>
                <div class="modal-body">
                    <div class="control-group">
                        <div class="controls">
                            <label class="control-label" for="txtProjectName"><abbr
                                    title="Project Name">Project Name</abbr></label>
                            <div class="controls">
                                <input type="text" class="input-xlarge" id="txtProjectName"
                                       name="txtProjectName" autofocus value="<?php echo $projectname ?>">
                            </div>
                        </div>
                        <div class="controls">
                            <label class="control-label" for="txtProjectDescription"><abbr
                                    title="Project Description">Description</abbr></label>
                            <div class="controls">
                                <textarea class="input-xlarge" id="txtProjectDescription"
                                          name="txtProjectDescription" rows="5"><?php echo $description ?></textarea>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="modal-footer">
                    <button class="btn btn-danger" onclick="DeleteProject()">Delete</button>
                    <button class="btn" data-dismiss="modal" aria-hidden="true">Cancel</button>
                    <button class="btn btn-primary" onclick="SaveProject()">Save</button>
                </div>
            </div>

            <!-- Placed at the end of the document so the pages load faster -->            
            <script src="assets/js/bootstrap-transition.js"></script>
            <script src="assets/js/bootstrap-alert.js"></script>
            <script src="assets/js/bootstrap-modal.js"></script>
            <script src="assets/js/bootstrap-dropdown.js"></script>
            <script src="assets/js/bootstrap-scrollspy.js"></script>
            <script src="assets/js/bootstrap-tab.js"></script>
            <script src="assets/js/bootstrap-tooltip.js"></script>
            <script src="assets/js/bootstrap-popover.js"></script>
            <script src="assets/js/bootstrap-button.js"></script>
            <script src="assets/js/bootstrap-collapse.js"></script>
            <script src="assets/js/bootstrap-carousel.js"></script>
            <script src="assets/js/bootstrap-typeahead.js"></script>

            <?php
        } else {
            header("location: login.php");
        }
        ?> 
    </body>
</html>